package com.cycplus.test

import android.Manifest
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import com.cycplus.test.base.BaseBindingActivity
import com.cycplus.test.databinding.ActivityMainBinding
import com.cycplus.test.ui.HighLightMoment
import com.cycplus.test.ui.HighLightType.HIGH_LIGHT_TYPE_CUSTOM
import com.cycplus.test.ui.VideoThumbnailLoader
import com.cycplus.test.ui.VideoThumbnailLoader.IFrameThumbLoaded
import com.cycplus.test.ui.VideoThumbnailTrackView
import com.cycplus.test.utils.ScreenUtil
import com.gyf.immersionbar.ImmersionBar


class MainActivity : BaseBindingActivity<ActivityMainBinding>(), IFrameThumbLoaded {

    override fun initImmersionBar() {
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.color_0f0f0f).navigationBarColor(R.color.color_0f0f0f)
            .autoDarkModeEnable(true).init()
    }

    override fun initView(rootView: View, savedInstanceState: Bundle?) {
        mBinding.btnBackgroundRender.setOnClickListener {
//            backgroundRender()
            mBinding.multiImageView.scrollToTime(20f)

            val a = HighLightMoment(HIGH_LIGHT_TYPE_CUSTOM, 0f, 20f, 22f, 25f, 5f, 2f, 3f)
            val b = HighLightMoment(HIGH_LIGHT_TYPE_CUSTOM, 0f, 80f, 85f, 90f, 10f, 5f, 5f)
            val c = HighLightMoment(HIGH_LIGHT_TYPE_CUSTOM, 0f, 160f, 170f, 180f, 20f, 10f, 10f)
            val list = arrayListOf<HighLightMoment>()
            list.add(a)
            list.add(b)
            list.add(c)
            mBinding.multiImageView.setVideoSliceList(list)

        }

        mBinding.btnAudioTrack.setOnClickListener {
//            playPCM()
            setImageview()
        }

        mBinding.multiImageView.setTimeChangeListener(object : VideoThumbnailTrackView.ITimeChangeListener{
            override fun onStartScroll() {

            }

            override fun onEndScroll() {

            }

            override fun onTimeChanged(timeSec: Float, userDrag: Boolean) {
//                Log.d("MainActivity", "timeChanged: $timeSec, userDrag: $userDrag")
            }
        })
        mBinding.multiImageView.setSliceListener(object : VideoThumbnailTrackView.ISliceListener {
            override fun onSliceClicked(slice: HighLightMoment) {

            }

            override fun addSlice(startSec: Float) {
                Log.d("MainActivity", "addSlice: $startSec")
            }

            override fun deleteSlice(slice: HighLightMoment) {
                Log.d("MainActivity", "deleteSlice: ${slice.startTimestamp}, ${slice.endTimestamp}")
            }

            override fun onSliceChanged(slice: HighLightMoment) {

            }
        })
    }

    override fun onFrameThumbLoaded(sec: Float, bitmap: Bitmap) {
        (mBinding.multiImageView as IFrameThumbLoaded).onFrameThumbLoaded(sec, bitmap)
    }

    private var videoThumbnailLoader: VideoThumbnailLoader? = null

    private fun setImageview(){
        if (videoThumbnailLoader == null) {
            videoThumbnailLoader = VideoThumbnailLoader("test.mp4", "111111", this)
        }
        mBinding.multiImageView.setVideoInfo(videoThumbnailLoader!!, 600f)
    }


    private fun backgroundRender(){
        val bitmap = Bitmap.createBitmap(ScreenUtil.getScreenWidth(this), ScreenUtil.dp2px(this, 200f), Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)

        val paint = Paint()
        paint.isAntiAlias = true
        paint.isDither = true

        paint.style = Paint.Style.STROKE
        paint.textSize = ScreenUtil.dp2px(this, 12f).toFloat()
        paint.setColor(Color.parseColor("#fe4545"))
        canvas.drawText("Hello", ScreenUtil.dp2px(this, 20f).toFloat(), ScreenUtil.dp2px(this, 20f).toFloat(), paint)

        paint.style = Paint.Style.FILL
        paint.setColor(Color.parseColor("#0b9c4c"))
        canvas.drawCircle(
            ScreenUtil.dp2px(this, 50f).toFloat(),
            ScreenUtil.dp2px(this, 50f).toFloat(),
            ScreenUtil.dp2px(this, 20f).toFloat(),
            paint)

        mBinding.img.setImageBitmap(bitmap)
    }

    private fun playPCM(){
        requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 100)
        val player = PcmPlayer()
        player.startPlaying("/storage/emulated/0/Download/111.pcm")
    }
}
